# -*- coding: utf-8 -*-
import logging
import time
from flask import request
from service.MonthRecommendService import insert_month_recommend, get_month_recommend_list, get_month_recommend
from flask import Blueprint

recommend_api = Blueprint('recommend_api', __name__)

@recommend_api.route('/movie/recommend', methods=['POST'])
def add_recommend():
    req_body = request.json
    title = req_body['title']
    content = req_body['content']
    created_time = time.strftime("%Y-%m-%d %H:%M:%S")

    recommend_list = [(title, content, created_time)]

    insert_month_recommend(recommend_list)
    logging.info("/movie/recommend/add接口调用成功")
    return {
        "meta": {"msg": "增加月度推荐成功", "status": 200}
    }

@recommend_api.route('/movie/recommend_list', methods=['GET'])
def get_recommend_list():
    recommend_list = get_month_recommend_list()
    logging.info("/movie/recommend/add接口调用成功")
    result = []
    for recommend in recommend_list:
        result.append({"id" : recommend[0], "title": recommend[1] , 'content' : recommend[2]})
    return {
        "data" : result,
        "meta": {"msg": "获取月度推荐成功", "status": 200}
    }

@recommend_api.route('/movie/month_recommend/<int:month_recommend_id>', methods=['GET'])
def get_recommend(month_recommend_id):
    month_recommend = get_month_recommend(month_recommend_id)
    logging.info("get /movie/recommend 接口调用成功")
    result = {"id" : month_recommend[0][0] , "title": month_recommend[0][1], "content": month_recommend[0][2] }
    return {
        "data" : result,
        "meta": {"msg": "获取月度推荐成功", "status": 200}
    }